package com.cong.leetcode.com.cong.hj.third;

import java.util.Arrays;
import java.util.Scanner;

public class Main {
    private static final int RED = 1;
    private static final int BLUE = -1;

    public static void main(String[] args) {
        Scanner in = new Scanner("5 5\n1 2 1 3 1 3 2 4 1 5\n");
        String size = in.nextLine();
        String[] sequence = in.nextLine().split(" ");

        int[][] pan = initPan(size);
        int seqIndex = 0;
        int cur = -1;
        for(;seqIndex < sequence.length; seqIndex++) {
            cur = cur == 1 ? -1 :1;
//            System.out.println("cur: " + cur);
            int col = Integer.parseInt(sequence[seqIndex]);
            // check error
            if (col > pan[0].length || col < 1 || pan[0][col - 1] != 0) {// error
                System.out.println((seqIndex + 1) + ",error");
                return;
            }
            // set cheese
            int row = 1;
            for(;row <= pan.length;row++) {
                if (row == pan.length || pan[row][col - 1] != 0) {
//                    System.out.println("set cheese: " + row + " " +col);
//                    System.out.println("SET: " + cur);
                    pan[row - 1][col - 1] = cur;
                    break;
                }
            }
            System.out.println("pan: \n" + Arrays.deepToString(pan));
            // judge win
//            System.out.println("judge: " + row + " " +col);
            if (judgeWin(pan, row - 1, col - 1)) {
//                System.out.println(Arrays.deepToString(pan));
                System.out.println((seqIndex + 1) + "," + (((seqIndex + 1 & 1) == 1) ? "red" : "blue"));
                return;
            }
        }
        System.out.println("0,draw");

    }
    private static boolean judgeWin(int[][]pan , int i, int j) {
//        if (i < 0 || i > pan.length - 1 || j < 0 || j > pan[0].length - 1) return false; // bound
        int curColor = pan[i][j];
        System.out.println("curColor: " + curColor);
        int leftCount = 0;
        int curI = i, curJ = j;
        while(true) {
            curJ--;
            if (curJ >= 0 && curColor == pan[i][curJ]){
                leftCount++;
            }else{
                break;
            }
        }

        int rCount = 0;
        curJ = j;
        while(true) {
            curJ++;
            if (curJ < pan[0].length && curColor == pan[i][curJ]){
                rCount++;
            }else{
                break;
            }
        }

        if (leftCount + rCount >= 3) {
            System.out.println("lr");
            return true;
        }

        int downCount = 0;
        curI = i;
        while(true) {
            curI++;
            if (curI < pan[0].length && curColor == pan[curI][j]){
                downCount++;
            }else{
                break;
            }
        }
        if (downCount >= 3) {
            System.out.println("down");
            return true;
        }

        curI = i;
        curJ = j;
        int rdCount = 0;
        while(true) {
            curI++;
            curJ++;
            if (curI < pan[0].length && curJ <= pan.length && curColor == pan[curI][curJ]){
                rdCount++;
            }else{
                break;
            }
        }
        if (rdCount >= 3) {
            System.out.println("rd");
            return true;
        }

        curI = i;
        curJ = j;
        int ldCount = 0;
        while(true) {
            curI++;
            curJ--;
            if (curI < pan[0].length && curJ >= 0 && curColor == pan[curI][curJ]){
                ldCount++;
            }else{
                break;
            }
        }
        if (ldCount >= 3) {
            System.out.println("ld");
            return true;
        }

        return false;
    }




    private static int[][] initPan(String size) {
        String[] sizeNM = size.split(" ");
        int height = Integer.parseInt(sizeNM[1]);
        int width = Integer.parseInt(sizeNM[0]);
        int[][] res = new int[height][width];

        return res;
    }
}
